﻿Namespace SetupDatastore
    Friend Class FGetDatastorePathAndPassword
        Friend Result As Result

        Public Shared Function CreateAndShowDialog(ByVal owner As IWin32Window, ByVal defaultPath As String, ByVal defaultPwd As String) As Result
            Dim clsView As New FGetDatastorePathAndPassword
            clsView.Icon = Framework.My.Resources.DB1
            clsView.PictureBox1.Image = Framework.My.Resources.Buddy.ToBitmap
            clsView.txtPath.Text = defaultPath
            clsView.txtPassword.Text = defaultPwd
            clsView.ShowDialog(owner)
            Return clsView.Result
        End Function


        Private Sub txtPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPath.TextChanged
            If IO.File.Exists(txtPath.Text) Then
                btnOK.Text = "&Connect"
                txtPassword2.Enabled = False
            Else
                btnOK.Text = "&Create"
                txtPassword2.Enabled = True
            End If
        End Sub

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Try
                If Not IO.File.Exists(txtPath.Text) Then
                    If txtPassword.Text <> txtPassword2.Text Then
                        MessageBox.Show(Me, "Passwords don't match!", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        txtPassword.Focus()
                        Return
                    End If
                End If
                If Manager.ValidateCreateOrUpgradeDb(Me, txtPath.Text, txtPassword.Text) Then
                    Result = New Result
                    Result.Path = txtPath.Text
                    Result.Password = txtPassword.Text
                    Me.Close()
                Else
                    txtPath.Focus()
                End If
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Result = Nothing
            Me.Close()
        End Sub

        Private Sub txtPath_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPath.Validating
            ValidatePath()
        End Sub
        Private Sub ValidatePath()
            txtPath.Text = txtPath.Text.Trim()
            If IO.Directory.Exists(txtPath.Text) Then
                txtPath.Text = System.IO.Path.Combine(txtPath.Text, "B2B.sdf")
            Else
                If Not txtPath.Text.ToUpper.EndsWith(".SDF") Then
                    txtPath.Text = txtPath.Text & ".sdf"
                End If
            End If
        End Sub

        Private Sub btnBrowseForFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseForFile.Click
            Try
                With OpenFileDialog1
                    .AddExtension = True
                    .AutoUpgradeEnabled = True
                    .CheckFileExists = False
                    .CheckPathExists = False
                    .FileName = txtPath.Text
                    .InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                    .Multiselect = False
                    .Filter = "SQL Server Compact Database File (*.sdf)|*.sdf"
                    .Title = "Browse for database file"
                    Select Case .ShowDialog(Me)
                        Case Windows.Forms.DialogResult.OK
                            txtPath.Text = .FileName
                            ValidatePath()
                            txtPassword.Focus()
                    End Select
                End With
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
    End Class
End Namespace